package com.yindong.camera.util;

import ohos.app.Context;
import ohos.media.image.common.Size;

import java.util.List;

/**
 * 相机比例计算工具类
 *
 * @author      殷冬
 * @date        2021-02-02
 * @version     1.0.0
 */
public class CameraRatioUtil {

    /**
     * 全屏
     */
    public static final Integer FULL_SCREEN = 1;

    /**
     * 4：3
     */
    public static final Integer FOUR_TO_THREE = 2;

    /**
     * 1：1
     */
    public static final Integer ONE_TO_ONE = 3;


    /**
     * 获取相机的宽度值
     *
     * @param context           上下文
     * @param ratioType         比例类型
     * @return
     */
    public static Size getCameraWidth(Context context, Integer ratioType){
        Size size = new Size();
        // 获取屏幕宽度像素值
        Integer width = ScreenPixelsUtil.getDisplayWidthInPx(context);
        size.width = width;
        if(ratioType.equals(FULL_SCREEN)){
            // 如果是全屏比例，直接返回屏幕高度
            size.height = ScreenPixelsUtil.getDisplayHeightInPX(context);
            return size;
        }
        if(ratioType.equals(FOUR_TO_THREE)){
            Double four = 4.0;
            Double three = 3.0;
            // 4:3比例
            Double height = width * (four / three);
            size.height = height.intValue();
            return size;
        }
        if(ratioType.equals(ONE_TO_ONE)){
            // 如果是1:1, 直接返回宽度值
            size.height = width;
            return size;
        }
        return null;
    }

    /**
     * 通过对比得到与宽高比最接近的预览尺寸（如果有相同尺寸，优先选择）
     *
     * @param isPortrait 是否竖屏
     * @param surfaceWidth 需要被进行对比的原宽
     * @param surfaceHeight 需要被进行对比的原高
     * @param preSizeList 需要对比的预览尺寸列表
     * @return 得到与原宽高比例最接近的尺寸
     */
    public static Size getOptimalPreviewSize(boolean isPortrait, List<Size> preSizeList, int surfaceWidth, int surfaceHeight) {
        int reqTmpWidth;
        int reqTmpHeight;
        // 当屏幕为垂直的时候需要把宽高值进行调换，保证宽大于高
        if (isPortrait) {
            reqTmpWidth = surfaceHeight;
            reqTmpHeight = surfaceWidth;
        } else {
            reqTmpWidth = surfaceWidth;
            reqTmpHeight = surfaceHeight;
        }
        //先查找preview中是否存在与surfaceview相同宽高的尺寸
        for(Size size : preSizeList){
            if((size.width == reqTmpWidth) && (size.height == reqTmpHeight)){
                return size;
            }
           /* if(size.height * reqTmpWidth / reqTmpHeight == size.width){
                return size;
            }*/
        }
        Size retSize = new Size();
        // 得到与传入的宽高比最接近的size
        float reqRatio = ((float) reqTmpWidth) / ((float) reqTmpHeight);
        float curRatio, deltaRatio;
        float deltaRatioMin = Float.MAX_VALUE;
        for (Size size : preSizeList) {
            curRatio = ((float) size.width) / size.height;
            deltaRatio = Math.abs(reqRatio - curRatio);
            if (deltaRatio < deltaRatioMin) {
                deltaRatioMin = deltaRatio;
                retSize = size;
            }
        }
        return retSize;
    }

}
